package com.cloudkinto.common.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author qiuy
 * @Title: PersonalInfoUtils
 * @ProjectName meijiequan-parent
 * @Description: TODO
 * @date 2018/11/13
 */
public class PersonalInfoUtils {


    /**
     * 邮箱验证
     * @param email
     * @return
     */
    public static boolean emailRule(String email){
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean matches = matcher.matches();
        return matches;
    }

    /**
     * 银行卡中间数字隐藏 使用 *  代替
     * @param accountNo
     * @return
     */
    public static String bankAccountNo(String accountNo){
        int length = accountNo.length();
        int beforeLength = 4;
        int afterLength = 4;

        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++) {
            if(i < beforeLength || i >= (length - afterLength)) {
                sb.append(accountNo.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }
        return sb.toString();
    }

    /**
     * 手机邮箱
     * @param accountNo
     * @param bagType
     * @return
     */
    public static String alipayAccountNo(String accountNo, Integer bagType){
        String str = "";
        if (accountNo != null && accountNo.contains("@") && bagType == 2){
            String firstStr = accountNo.substring(0,3);
            String lastStr = accountNo.substring(accountNo.lastIndexOf("@") - 2);
            str = firstStr + "****" +lastStr;
        }
        if (accountNo != null && !accountNo.contains("@") && bagType == 2){
            str = accountNo.substring(0, 3) + "****" + accountNo.substring(7, accountNo.length());
        }
        return str;
    }

    /**
     * 姓名打星
     * @param userTrueName
     * @return
     */
    public static String userTrueName(String userTrueName){
        String userName = "";
        if(userTrueName.length() == 2){
            userName =  userTrueName.replaceFirst(userTrueName.substring(1),"*");
        }
        if (userTrueName.length() > 2) {
            userName =  userTrueName.replaceFirst(userTrueName.substring(1,userTrueName.length() - 1) ,"*");
        }
        return userName;
    }

    /**
     * 手机正则验证
     * @param mobile
     * @return
     */
    public static boolean mobileCode(String mobile) {
        String check = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(mobile);
        boolean matches = matcher.matches();
        return matches;
    }
}
